package com.nowcoder.community.controller;

import com.nowcoder.community.event.EventProducer;
import com.nowcoder.community.pojo.Comment;
import com.nowcoder.community.pojo.DiscussPost;
import com.nowcoder.community.pojo.Event;
import com.nowcoder.community.service.CommentService;
import com.nowcoder.community.service.DiscussPostService;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.HostHolder;
import com.nowcoder.community.util.RedisKeyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

@Api(description = "评论相关")
@Controller
@RequestMapping("/comment")
public class CommentController implements CommunityConstant{

    @Autowired
    @Qualifier("commentService")
    private CommentService commentService;

    @Autowired
    @Qualifier("discussPostService")
    private DiscussPostService discussPostService;

    @Autowired
    private EventProducer producer;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "添加评论")
    @PostMapping("/add/{discussPostId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "discussPostId", value = "帖子id"),
            @ApiImplicitParam(name = "comment", value = "评论实例")
    })
    public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment){

        //获取当前用户的id，注入到评论的user_id属性中
        comment.setUserId(hostHolder.getUser().getId());

        comment.setStatus(0);

        comment.setCreateTime(new Date());

        commentService.addComment(comment);


        //上面的操作完成后，触发评论事件（添加评论后，就会显示xxx发表了评论的相关信息）
        Event event = new Event()
                .setTopic(TOPIC_COMMENT)//事件的主题为评论
                .setUserId(hostHolder.getUser().getId())
                .setEntityType(comment.getEntityType())
                .setEntityId(comment.getEntityId())
                .setData("discussPostId", discussPostId);//在哪个帖子上发表评论

        //设置事件中评论的具体类型
        if(comment.getEntityType() == ENTITY_TYPE_POST){
            //帖子
            DiscussPost target = discussPostService.findDiscussPostById(comment.getEntityId());
            event.setEntityUserId(target.getUserId());
        }else if(comment.getEntityType() == ENTITY_TYPE_COMMENT){
            //评论
            Comment target = commentService.findCommentById(comment.getEntityId());
            event.setEntityUserId(target.getUserId());
        }

        //生产者发布事件，将事件放在消息队列中等待消费者线程来取，而生产者线程发布事件后继续执行其他的操作。（并发）
        producer.fireEvent(event);

        if (comment.getEntityType() == ENTITY_TYPE_POST){
            //触发评论事件
            event = new Event()
                    .setTopic(TOPIC_PUBLISH)
                    .setUserId(comment.getUserId())
                    .setEntityType(ENTITY_TYPE_POST)
                    .setEntityId(discussPostId);
            producer.fireEvent(event);

            //计算帖子分数
            String redisKey = RedisKeyUtil.getPostScoreKey();
            redisTemplate.opsForSet().add(redisKey, discussPostId);
        }

        return "redirect:/discusspost/detail/" + discussPostId;
    }
}
